//
//  LCShowComposeController.swift
//  JKBiblicalReading
//
//  Created by admin on 2018/8/17.
//  Copyright © 2018年 LC. All rights reserved.
//

import UIKit
import SDAutoLayout
import MJRefresh

let showComposeViewNoImagesCell_Identifier = "showComposeViewNoImagesCell_Identifier"
let showComposeViewCell_Identifier = "showComposeViewCell_Identifier"

class LCShowComposeController: LCViewController{
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()

    override func viewDidLoad() {
        super.viewDidLoad()
        edgesForExtendedLayout = UIRectEdge.init(rawValue: 0);
        tabBarController?.tabBar.isTranslucent = false;
        view.backgroundColor = UIColor.white
        title = "谈文论经"
        view.addSubview(composeTopView)
        composeTopView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(0)
            make.left.right.equalTo(view)
            make.bottom.equalTo(composeTopView.titleLabel.snp.bottom).offset(20)
        }
        view.addSubview(composeTableView)
        composeTableView.snp.makeConstraints { (make) in
            make.top.equalTo(composeTopView.snp.bottom).offset(0)
            make.left.right.equalTo(view)
            make.bottom.equalTo(view.snp.bottom)
        }
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        self.composeTableView.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        self.composeTableView.mj_footer = footer
        loadData()
    }
    // 顶部刷新
    @objc func headerRefresh(){
        self.showComposeViewModel.page = 1
        self.loadData()     // 结束刷新
        self.footer.resetNoMoreData()
    }
    // 底部刷新
    @objc func footerRefresh(){
        self.showComposeViewModel.page = self.showComposeViewModel.page + 1
        self.loadData()        // 结束刷新
    }
    deinit {
        print("deinit");
    }
    //底部视图
    fileprivate lazy var composeTableView:UITableView = {
        let view = UITableView()
        self.setupComposeTableView(view)//设置
        return view
    }()
    //FIXME:- 下午4:57///设置LCComposeTopView 节圣经
    fileprivate func setupComposeTableView(_ tableView : UITableView){
        //FIXME:- // 设置列表内容
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(LCShowComposeViewNoImagesCellStyle1.self, forCellReuseIdentifier: showComposeViewNoImagesCell_Identifier)
        tableView.register(LCShowComposeViewCell.self, forCellReuseIdentifier: showComposeViewCell_Identifier)
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
    }

    //顶部视图
    fileprivate lazy var composeTopView:LCComposeTopView = {
        let view = LCComposeTopView()
        self.setupComposeTextView(view)//设置
        return view
    }()
    //FIXME:- 下午4:57///设置顶部视图 LCComposeTopView
    fileprivate func setupComposeTextView(_ view : LCComposeTopView){
        //FIXME:- // 设置文字内容
        view.placeholderLabel.text = "[" + jieStr + "] " + contentStr;
        view.titleLabel.text = "一一" + titleStr
    }
    lazy var showComposeViewModel : LCShowComposeViewModel = {
        let vm = LCShowComposeViewModel()
        return vm
    }()
    @objc public lazy var titleStr : String = {
        let str : String = String()
        return str
    }()
    @objc public lazy var jieStr : String = {
        let str : String = String()
        return str
    }()
    @objc public lazy var contentStr : String = {
        let str : String = String()
        return str
    }()
}
extension LCShowComposeController: UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.cellHeight(for: indexPath, cellContentViewWidth: UIScreen.main.bounds.size.width, tableView: tableView)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return showComposeViewModel.dataArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let showComposeModel = self.showComposeViewModel.dataArray[indexPath.row];
        
        if true
            /**(showComposeModel as? LCShowComposeModel)?.images == nil
             || (showComposeModel as? LCShowComposeModel)?.images.count == 0*/ {
            let noImagesCell:LCShowComposeViewNoImagesCellStyle1 = tableView.dequeueReusableCell(withIdentifier: showComposeViewNoImagesCell_Identifier) as! LCShowComposeViewNoImagesCellStyle1
            noImagesCell.model = (showComposeModel as? LCShowComposeModel)
            noImagesCell.selectionStyle = .none
            //showComposeViewModel :
            noImagesCell.authorImageView.tapGesture { [weak self] (_)  in
                self?.addtimeButtonClick(showComposeModel: showComposeModel as! LCShowComposeModel)
            }
            noImagesCell.usernameButton.tapGesture { [weak self] (_) in
                self?.addtimeButtonClick(showComposeModel: showComposeModel as! LCShowComposeModel)
            }

            return noImagesCell
        }
        
        let cell:LCShowComposeViewCell = tableView.dequeueReusableCell(withIdentifier: showComposeViewCell_Identifier) as! LCShowComposeViewCell
        cell.model = (showComposeModel as? LCShowComposeModel)
        cell.selectionStyle = .none
        cell.authorImageView.tapGesture { [weak self] (_)  in
            self?.addtimeButtonClick(showComposeModel: showComposeModel as! LCShowComposeModel)
        }
        cell.usernameButton.tapGesture { [weak self] (_) in
            self?.addtimeButtonClick(showComposeModel: showComposeModel as! LCShowComposeModel)
        }

        return cell
        

    }
    
    
    @objc fileprivate func addtimeButtonClick(showComposeModel : LCShowComposeModel){
        print("地区和add时间")
        let vc: LSZPersonalInformationPageVC = LSZPersonalInformationPageVC()
        vc.uid = showComposeModel.uid;
        vc.imageViewHeader = showComposeModel.avatar;
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension LCShowComposeController{
    func endRefreshing(){
        self.composeTableView.mj_header.endRefreshing()
        self.composeTableView.mj_footer.endRefreshing()
    }

    fileprivate func loadData(){
        showComposeViewModel.getComposeTextData(withType: 1, shengJingTitle: titleStr, notesContentArr: [jieStr+" "+contentStr], success: {[weak self] (res) in
            
            if (self?.showComposeViewModel.dataArray.count)! > 0 {
                self?.endRefreshing()
                self?.composeTableView.reloadData()
            }else{
                print("请求失败");
            }
        },failure: {[weak self] (error) in
                self?.endRefreshing()
                print(error ?? "")
                print("请求失败");
        }) {[weak self] () in
            self?.footer.endRefreshingWithNoMoreData()
        }
    }

}
